﻿using System;
using System.Collections.Generic;
using System.Text;
using Y.Common;
namespace RedisInAction
{

    /// <summary>
    /// Redis实践单号生成
    /// </summary>
    public class GenerationNO
    {
        public static string GetNO(SysNoConfig config)
        {
            string date = "";
            if (!config.fserialdate.IsVoid())
            {
                date = DateTime.Now.ToString(config.fserialdate);
            }
            var index = RedisService.RedisFactory.DefaultRedis.StringIncr($"{config.fprefix}{date}");
            string SerialNo = index.ToString("D" + config.fseriallength);
            if (SerialNo.Length > config.fseriallength)
            {
                throw new Exception("获取编号失败,已超出长度！");
            }
            string randomNo = "";
            if (config.frandomlength > 0)
            {
                randomNo = Y.Common.RandomString.RandomDataNum(config.frandomlength);
            }
            return $"{config.fprefix}{date}{SerialNo}{randomNo}";
        }

    }
}
